package com.hy.controller;

import com.hy.common.Result;
import com.hy.dto.user.HeatMapData;
import com.hy.entity.UserAddress;
import com.hy.service.UserAddressService;
import com.hy.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/addresses")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private UserUtil userUtil;

    /**
     * 获取用户地址列表
     */
    @GetMapping
    public Result<List<UserAddress>> list(HttpServletRequest request) {
        return Result.success(userAddressService.getUserAddresses());
    }

    /**
     * 添加地址
     */
    @PostMapping("/save")
    public Result<Void> add(@RequestBody UserAddress address) {
        return userAddressService.saveAddress(address);
    }

    /**
     * 修改地址
     */
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody UserAddress address, HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        address.setId(id);
        address.setUserId(userId);
        return Result.success(userAddressService.updateById(address));
    }

    /**
     * 设置默认地址
     */
    @PostMapping("/{id}/default")
    public Result<Boolean> setDefault(@PathVariable Long id) {
        return Result.success(userAddressService.setDefault(id));
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(userAddressService.delete(id));
    }

    /**
     * 获取省份级别热力图数据
     */
    @GetMapping("/province")
    public Result<List<HeatMapData>> getProvinceHeatMap() {
        return Result.success(userAddressService.getProvinceHeatMapData());
    }

    /**
     * 获取城市级别热力图数据
     */
    @GetMapping("/city")
    public Result<List<HeatMapData>> getCityHeatMap() {
        return Result.success(userAddressService.getCityHeatMapData());
    }

    /**
     * 获取区县级别热力图数据
     */
    @GetMapping("/district")
    public Result<List<HeatMapData>> getDistrictHeatMap() {
        return Result.success(userAddressService.getDistrictHeatMapData());
    }

    /**
     * 获取所有用户位置点数据
     */
    @GetMapping("/locations")
    public Result<List<HeatMapData>> getAllUserLocations() {
        return Result.success(userAddressService.getAllUserLocations());
    }

    /**
     * 获取省份列表及用户分布
     */
    @GetMapping("/provinceUser")
    public Map<String, Object> getProvinceList() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> provinces = userAddressService.getProvinceUserDistribution();
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", provinces);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取指定省份下的城市列表及用户分布
     */
    @GetMapping("/cityUser")
    public Map<String, Object> getCityList(@RequestParam Long provinceId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> cities = userAddressService.getCityUserDistribution(provinceId);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", cities);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取指定城市下的区县列表及用户分布
     */
    @GetMapping("/districtUser")
    public Map<String, Object> getDistrictList(@RequestParam Long cityId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> districts = userAddressService.getDistrictUserDistribution(cityId);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", districts);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取指定省份下的用户列表
     */
    @GetMapping("/province/users")
    public Map<String, Object> getUsersByProvince(@RequestParam Long provinceId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> users = userAddressService.getUsersByProvince(provinceId);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", users);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取指定城市下的用户列表
     */
    @GetMapping("/city/users")
    public Map<String, Object> getUsersByCity(@RequestParam Long cityId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> users = userAddressService.getUsersByCity(cityId);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", users);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取指定区县下的用户列表
     */
    @GetMapping("/district/users")
    public Map<String, Object> getUsersByDistrict(@RequestParam Long districtId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> users = userAddressService.getUsersByDistrict(districtId);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", users);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
}
